//
//  LPOneViewCanChooseMoreView.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/11/24.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPOneViewCanChooseMoreView.h"

@interface LPOneViewCanChooseMoreView ()
/** label */
@property (nonatomic, strong) UILabel *label;

@property (nonatomic, strong) UIButton *button;
/**  自取方式  */
@property (assign, nonatomic) BOOL   isByMyself;
/**  快递方式  */
@property (assign, nonatomic) BOOL   isKuaiDi;

@end
@implementation LPOneViewCanChooseMoreView

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title chooseName:(NSArray *)names
{
    self = [super initWithFrame:frame];
    if (self) {
        self.isByMyself = NO;
        self.isKuaiDi = NO;
        [self addSubview:self.label];
        self.label.text = title;
        CGFloat width = [CalculateLabelHeight CalculateLabelWidthWithString:title font:16 * [UIScreen zj_textFontScale] height:20 * [UIScreen zj_screenHScale]].width;
        self.label.sd_layout
        .leftSpaceToView(self, 15 * [UIScreen zj_screenWScale])
        .heightIs(20 * [UIScreen zj_screenHScale])
        .widthIs(width)
        .centerYEqualToView(self);
        
        for (int i = 0; i < names.count; i ++) {
            NSString *name = names[i];
            CGFloat width1 = [CalculateLabelHeight CalculateLabelWidthWithString:name font:16 * [UIScreen zj_textFontScale] height:20 * [UIScreen zj_screenHScale]].width;
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(width + 65 * [UIScreen zj_screenWScale] + i * (width1 + 50 * [UIScreen zj_screenWScale]), 15 * [UIScreen zj_screenHScale], width1 + 40 * [UIScreen zj_screenWScale], 20 * [UIScreen zj_screenHScale])];
            button.titleLabel.font = LYPFONT(16);
            [button setTitle:name forState:UIControlStateNormal];
            [button setTitleColor:[UIColor textGrayColor] forState:UIControlStateNormal];
            button.tag = i;
            [button setImage:[UIImage imageNamed:@"option_default"] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"option_normal"] forState:UIControlStateSelected];
           
            [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:button];
            if (i == 0) {
                button.selected = YES;
                self.isByMyself = YES;
            }
        }
        
    }
    return self;
}


- (void)clickButton:(UIButton *)sender{
    if (sender.tag == 0) {
        if (sender.isSelected) {
            sender.selected = NO;
        }else{
             sender.selected = YES;
        }
        self.isByMyself = sender.isSelected;
    }else{
        if (sender.isSelected) {
            sender.selected = NO;
        }else{
            sender.selected = YES;
        }
        self.isKuaiDi = sender.isSelected;
    }
    if (self.delegate) {
        [self.delegate LPOneViewCanChooseMoreViewziti:self.isByMyself kuaidi:self.isKuaiDi];
    }
}

#pragma mark - getter

- (UILabel *)label{
    if (!_label) {
        _label = ({
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor textBlackColor];
            label.backgroundColor = [UIColor clearColor];
            label.font = LYPFONT(16);
            label;
        });
    }
    return _label;
}

@end
